package stream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 分组(partitioningBy/groupingBy)
 * 分区：将stream按条件分为两个Map，比如员工按薪资是否高于8000分为两部分。
 * 分组：将集合分为多个Map，比如员工按性别分组。有单级分组和多级分组。
 *
 * @author Hongliang Zhu
 * @create 2021-08-01 16:11
 */
public class Demo9 {
    public static void main(String[] args) {

        List<Person> personList = new ArrayList();
        personList.add(new Person("Tom", 8900, "male", "New York"));
        personList.add(new Person("Jack", 7000, "male", "Washington"));
        personList.add(new Person("Lily", 7800, "female", "Washington"));
        personList.add(new Person("Anni", 8200, "female", "New York"));
        personList.add(new Person("Owen", 9500, "male", "New York"));
        personList.add(new Person("Alisa", 7900, "female", "New York"));

        // 将员工按薪资是否高于8000分组
        Map<Boolean, List<Person>> part  = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
        System.out.println(part.get(true));

        // 将员工按性别分组
        Map<String, List<Person>> group  = personList.stream().collect(Collectors.groupingBy(Person::getSex));
        System.out.println(group);

        // 将员工先按性别分组，再按地区分组
        Map<String, Map<String, List<Person>>> collect = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
        System.out.println(collect);

    }
}
